package com.myfp.myfund;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import com.httputils.AndroidCommFactory;
import com.httputils.CacheBean;
import com.httputils.ConnMachine;
import com.httputils.CrashHandler;
import com.httputils.NetworkConstant;
import com.utils.NetworkUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by john on 2015/11/15.
 */
public class AppBean extends Application{

    private boolean netStat = false;// 当前网络状态
    private AndroidCommFactory factory;// 联网器工厂类
    public HashMap<String, ConnMachine> hs;// 存储url和对应machine的hashmap
    private CacheBean cacheBean;// 缓存Bean，可用于Activity之间的数据传输，当模块切换时需要将缓存Bean的数据进行统一清除调用clear方法
    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private final Lock r = rwl.readLock();
    private final Lock w = rwl.writeLock();
    private List<Activity> activitesList;
    // 网络状态监听广播
    private BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setNetStat(NetworkUtils.isNetworkConnected(context));// 设置当前网络状态
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化未捕获异常类
        CrashHandler handler = CrashHandler.getInstance();
        handler.init(this);
        //初始化未捕获异常类
        factory = new AndroidCommFactory();
        hs = new HashMap<String, ConnMachine>();
        cacheBean = CacheBean.getInstance();
        setNetStat(NetworkUtils.isNetworkConnected(this));//设置当前网络状态
        activitesList = new ArrayList<Activity>();
        // 注册网络状态检测监听广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(connectionReceiver, intentFilter);

        //SDKInitializer.initialize(this);
    }

    public List<Activity> getActivitesList() {
        return activitesList;
    }

    public void setActivitesList(List<Activity> activitesList) {
        this.activitesList = activitesList;
    }

    public boolean isNetStat() {
        return netStat;
    }

    public void setNetStat(boolean netStat) {
        this.netStat = netStat;
    }

    // 获取一个联网器实例
    public ConnMachine getCommMachine() {
        if (factory != null) {
            return factory.CreateNewCommMachine(NetworkConstant.COMM_TYPE);
        } else {
            factory = new AndroidCommFactory();
            return factory.CreateNewCommMachine(NetworkConstant.COMM_TYPE);
        }
    }

    public void putUrl(String url, ConnMachine machine) {
        w.lock();
        try {
            hs.put(url, machine);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            w.unlock();
        }
    }

    public void clearPoint(String url) {
        w.lock();
        try {
            hs.remove(url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            w.unlock();
        }
    }

    // 通过url获取对应machine
    public ConnMachine getMachine(String url) {
        r.lock();
        ConnMachine machine = null;
        try {
            machine = hs.get(url);
            return machine;
        } catch (Exception e) {
            e.printStackTrace();
            return machine;
        } finally {
            r.unlock();
        }

    }

    public CacheBean getCacheBean() {
        return cacheBean;
    }
}
